/*
Consider there is an array with duplicates and u r given two numbers as input and u have to return the minimum distance between the two in the array with minimum complexity.
*/
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

int getMindis(int num[], int size, int a, int b)
{
    int minDis = 100000;
    int lasta = -1, lastb = -1;
    
    for (int i = 0; i < size; i++) {
        if (num[i] != a && num[i] != b) continue;
        if (num[i] == a) {
            if (lastb != -1) minDis = min(minDis, i - lastb); 
            lasta = i;
        }
        if (num[i] == b) {
            if (lasta != -1) minDis = min(minDis, i - lasta);
            lastb = i;
        }
    }  
    return minDis;
}

pos1 = pos2 = dis = INT_MAX;
for(int i = 0; i < n; i++)
{
   if (a[i] == num1) pos1 = i ;
   if (a[i] == num2) pos2 = i ;
   if (pos1 < INT_MAX && pos2 < INT_MAX)
       dis = min (dis, abs(pos1-pos2) );   
}



int main(int argc, char **argv)
{
    int A[] = {1,4,6,7,9,5,3,7,8,9,3,2,5,7,4,3,15,4};
    cout << getMindis(A, sizeof(A)/sizeof(int), 4, 5) << endl;
}
